package cn.z2huo.mybatis.generator.mybatis3dynamicsql.dao;

import cn.z2huo.demo.model.dataobject.Role;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.delete.DeleteDSLCompleter;
import org.mybatis.dynamic.sql.select.CountDSLCompleter;
import org.mybatis.dynamic.sql.select.SelectDSLCompleter;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.mybatis.dynamic.sql.update.UpdateDSL;
import org.mybatis.dynamic.sql.update.UpdateDSLCompleter;
import org.mybatis.dynamic.sql.update.UpdateModel;
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
import org.mybatis.dynamic.sql.util.mybatis3.*;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

import static cn.z2huo.mybatis.generator.mybatis3dynamicsql.dao.RoleDynamicSqlSupport.*;
import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;

@Mapper
public interface RoleMapper extends CommonCountMapper, CommonDeleteMapper, CommonInsertMapper<Role>, CommonUpdateMapper {
    BasicColumn[] selectList = BasicColumn.columnList(id, createTime, operateTime, createByCode, operateByCode, roleName, roleCode, ranking, validFlag, deleteFlag, deleteTime, systemId);

    @SelectProvider(type=SqlProviderAdapter.class, method="select")
    @Results(id="RoleResult", value = {
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="operate_time", property="operateTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="create_by_code", property="createByCode", jdbcType=JdbcType.VARCHAR),
        @Result(column="operate_by_code", property="operateByCode", jdbcType=JdbcType.VARCHAR),
        @Result(column="role_name", property="roleName", jdbcType=JdbcType.VARCHAR),
        @Result(column="role_code", property="roleCode", jdbcType=JdbcType.VARCHAR),
        @Result(column="ranking", property="ranking", jdbcType=JdbcType.INTEGER),
        @Result(column="valid_flag", property="validFlag", jdbcType=JdbcType.VARCHAR),
        @Result(column="delete_flag", property="deleteFlag", jdbcType=JdbcType.VARCHAR),
        @Result(column="delete_time", property="deleteTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="system_id", property="systemId", jdbcType=JdbcType.BIGINT)
    })
    List<Role> selectMany(SelectStatementProvider selectStatement);

    @SelectProvider(type=SqlProviderAdapter.class, method="select")
    @ResultMap("RoleResult")
    Optional<Role> selectOne(SelectStatementProvider selectStatement);

    default long count(CountDSLCompleter completer) {
        return MyBatis3Utils.countFrom(this::count, role, completer);
    }

    default int delete(DeleteDSLCompleter completer) {
        return MyBatis3Utils.deleteFrom(this::delete, role, completer);
    }

    default int deleteByPrimaryKey(Long id_) {
        return delete(c -> 
            c.where(id, isEqualTo(id_))
        );
    }

    default int insert(Role row) {
        return MyBatis3Utils.insert(this::insert, row, role, c ->
            c.map(id).toProperty("id")
            .map(createTime).toProperty("createTime")
            .map(operateTime).toProperty("operateTime")
            .map(createByCode).toProperty("createByCode")
            .map(operateByCode).toProperty("operateByCode")
            .map(roleName).toProperty("roleName")
            .map(roleCode).toProperty("roleCode")
            .map(ranking).toProperty("ranking")
            .map(validFlag).toProperty("validFlag")
            .map(deleteFlag).toProperty("deleteFlag")
            .map(deleteTime).toProperty("deleteTime")
            .map(systemId).toProperty("systemId")
        );
    }

    default int insertMultiple(Collection<Role> records) {
        return MyBatis3Utils.insertMultiple(this::insertMultiple, records, role, c ->
            c.map(id).toProperty("id")
            .map(createTime).toProperty("createTime")
            .map(operateTime).toProperty("operateTime")
            .map(createByCode).toProperty("createByCode")
            .map(operateByCode).toProperty("operateByCode")
            .map(roleName).toProperty("roleName")
            .map(roleCode).toProperty("roleCode")
            .map(ranking).toProperty("ranking")
            .map(validFlag).toProperty("validFlag")
            .map(deleteFlag).toProperty("deleteFlag")
            .map(deleteTime).toProperty("deleteTime")
            .map(systemId).toProperty("systemId")
        );
    }

    default int insertSelective(Role row) {
        return MyBatis3Utils.insert(this::insert, row, role, c ->
            c.map(id).toPropertyWhenPresent("id", row::getId)
            .map(createTime).toPropertyWhenPresent("createTime", row::getCreateTime)
            .map(operateTime).toPropertyWhenPresent("operateTime", row::getOperateTime)
            .map(createByCode).toPropertyWhenPresent("createByCode", row::getCreateByCode)
            .map(operateByCode).toPropertyWhenPresent("operateByCode", row::getOperateByCode)
            .map(roleName).toPropertyWhenPresent("roleName", row::getRoleName)
            .map(roleCode).toPropertyWhenPresent("roleCode", row::getRoleCode)
            .map(ranking).toPropertyWhenPresent("ranking", row::getRanking)
            .map(validFlag).toPropertyWhenPresent("validFlag", row::getValidFlag)
            .map(deleteFlag).toPropertyWhenPresent("deleteFlag", row::getDeleteFlag)
            .map(deleteTime).toPropertyWhenPresent("deleteTime", row::getDeleteTime)
            .map(systemId).toPropertyWhenPresent("systemId", row::getSystemId)
        );
    }

    default Optional<Role> selectOne(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectOne(this::selectOne, selectList, role, completer);
    }

    default List<Role> select(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectList(this::selectMany, selectList, role, completer);
    }

    default List<Role> selectDistinct(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectDistinct(this::selectMany, selectList, role, completer);
    }

    default Optional<Role> selectByPrimaryKey(Long id_) {
        return selectOne(c ->
            c.where(id, isEqualTo(id_))
        );
    }

    default int update(UpdateDSLCompleter completer) {
        return MyBatis3Utils.update(this::update, role, completer);
    }

    static UpdateDSL<UpdateModel> updateAllColumns(Role row, UpdateDSL<UpdateModel> dsl) {
        return dsl.set(id).equalTo(row::getId)
                .set(createTime).equalTo(row::getCreateTime)
                .set(operateTime).equalTo(row::getOperateTime)
                .set(createByCode).equalTo(row::getCreateByCode)
                .set(operateByCode).equalTo(row::getOperateByCode)
                .set(roleName).equalTo(row::getRoleName)
                .set(roleCode).equalTo(row::getRoleCode)
                .set(ranking).equalTo(row::getRanking)
                .set(validFlag).equalTo(row::getValidFlag)
                .set(deleteFlag).equalTo(row::getDeleteFlag)
                .set(deleteTime).equalTo(row::getDeleteTime)
                .set(systemId).equalTo(row::getSystemId);
    }

    static UpdateDSL<UpdateModel> updateSelectiveColumns(Role row, UpdateDSL<UpdateModel> dsl) {
        return dsl.set(id).equalToWhenPresent(row::getId)
                .set(createTime).equalToWhenPresent(row::getCreateTime)
                .set(operateTime).equalToWhenPresent(row::getOperateTime)
                .set(createByCode).equalToWhenPresent(row::getCreateByCode)
                .set(operateByCode).equalToWhenPresent(row::getOperateByCode)
                .set(roleName).equalToWhenPresent(row::getRoleName)
                .set(roleCode).equalToWhenPresent(row::getRoleCode)
                .set(ranking).equalToWhenPresent(row::getRanking)
                .set(validFlag).equalToWhenPresent(row::getValidFlag)
                .set(deleteFlag).equalToWhenPresent(row::getDeleteFlag)
                .set(deleteTime).equalToWhenPresent(row::getDeleteTime)
                .set(systemId).equalToWhenPresent(row::getSystemId);
    }

    default int updateByPrimaryKey(Role row) {
        return update(c ->
            c.set(createTime).equalTo(row::getCreateTime)
            .set(operateTime).equalTo(row::getOperateTime)
            .set(createByCode).equalTo(row::getCreateByCode)
            .set(operateByCode).equalTo(row::getOperateByCode)
            .set(roleName).equalTo(row::getRoleName)
            .set(roleCode).equalTo(row::getRoleCode)
            .set(ranking).equalTo(row::getRanking)
            .set(validFlag).equalTo(row::getValidFlag)
            .set(deleteFlag).equalTo(row::getDeleteFlag)
            .set(deleteTime).equalTo(row::getDeleteTime)
            .set(systemId).equalTo(row::getSystemId)
            .where(id, isEqualTo(row::getId))
        );
    }

    default int updateByPrimaryKeySelective(Role row) {
        return update(c ->
            c.set(createTime).equalToWhenPresent(row::getCreateTime)
            .set(operateTime).equalToWhenPresent(row::getOperateTime)
            .set(createByCode).equalToWhenPresent(row::getCreateByCode)
            .set(operateByCode).equalToWhenPresent(row::getOperateByCode)
            .set(roleName).equalToWhenPresent(row::getRoleName)
            .set(roleCode).equalToWhenPresent(row::getRoleCode)
            .set(ranking).equalToWhenPresent(row::getRanking)
            .set(validFlag).equalToWhenPresent(row::getValidFlag)
            .set(deleteFlag).equalToWhenPresent(row::getDeleteFlag)
            .set(deleteTime).equalToWhenPresent(row::getDeleteTime)
            .set(systemId).equalToWhenPresent(row::getSystemId)
            .where(id, isEqualTo(row::getId))
        );
    }
}